DECLARE @CNT INT
DECLARE @StudentAutoID INT;
DECLARE @maxRandomValue TINYINT = 64
	  , @minRandomValue TINYINT = 1;
DECLARE @CourseID1 TINYINT;
DECLARE @CourseID2 TINYINT;
DECLARE @CourseID3 TINYINT;
DECLARE @CID1 INT = -1;
DECLARE @CID2 INT = -1;
DECLARE @CID3 INT = -1;

SET @CNT = 1


WHILE(@CNT < 1141) --Maximum Auto Id in Student Table 
BEGIN
	SET @StudentAutoId = -1;
	SET @CID1 = -1;
	SET @CID2 = -1;
	SET @CID3 = -1;
	SELECT @StudentAutoID = AutoID from Student where AutoId = @CNT;
    if(@StudentAutoID != -1)
		BEGIN
			--PRINT @StudentAutoId;
			--Choose random courses from Course table
			SELECT @CourseID1 = CAST(((@maxRandomValue + 1) - @minRandomValue) 
				* RAND() + @minRandomValue AS TINYINT);
			SELECT @CourseID2 = CAST(((@maxRandomValue + 1) - @minRandomValue) 
				* RAND() + @minRandomValue AS TINYINT);
			SELECT @CourseID3 = CAST(((@maxRandomValue + 1) - @minRandomValue) 
				* RAND() + @minRandomValue AS TINYINT);

			PRINT CAST(@StudentAutoID AS NVARCHAR)+ CAST(@CourseID1 AS NVARCHAR)+'   '+CAST(@CourseID2 AS NVARCHAR)+'   '+CAST(@CourseID3 AS NVARCHAR);
			
			SELECT @CID1 = AutoID from Course where AutoId = @CourseID1;
			SELECT @CID2 = AutoID from Course where AutoId = @CourseID2;
			SELECT @CID3 = AutoID from Course where AutoId = @CourseID3;
			IF(@CID1 != -1)
				INSERT INTO CourseRegistration (StudentAutoId,CourseAutoId) Values(@StudentAutoID, @CID1);
			IF(@CID2 != -1)
				INSERT INTO CourseRegistration (StudentAutoId,CourseAutoId) Values(@StudentAutoID, @CID2);
			IF(@CID3 != -1)
				INSERT INTO CourseRegistration (StudentAutoId,CourseAutoId) Values(@StudentAutoID, @CID3);
			
		END
    SET @CNT = @CNT + 1;

END


